#
# Copyright (C) 2013 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=gutenprint_lede
PKG_VERSION:=5.3.5
PKG_SUBVERSION:=pre1
PKG_RELEASE:=1

PKG_SRC_NAME:=gutenprint

PKG_SOURCE:=$(PKG_SRC_NAME)-$(PKG_VERSION)-$(PKG_SUBVERSION).tar.xz
PKG_SOURCE_URL:=@SF/gimp-print/
PKG_MD5SUM:=ffefd429ab3bee13f0e409f42f53ba7d

PKG_BUILD_DEPENDS:=gutenprint_lede/host cups_lede ghostscript_lede libiconv-full
HOST_BUILD_DEPENDS:=libiconv-full/host

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SRC_NAME)-$(PKG_VERSION)-$(PKG_SUBVERSION)
HOST_BUILD_DIR:=$(BUILD_DIR_HOST)/$(PKG_SRC_NAME)-$(PKG_VERSION)-$(PKG_SUBVERSION)

PKG_BUILD_DEPENDS:=gutenprint_lede/host cups_lede libiconv-full
HOST_BUILD_DEPENDS:=libiconv-full
PKG_INSTALL:=1

include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/nls.mk

define Package/gutenprint_lede
  SECTION:=utils
  SUBMENU:=Utilities
  TITLE:=(LEDE) Gutenprint CUPS drivers
  # XXX FIXME perl is required by cups-genppdupdate, but it needs more libs
  DEPENDS:=+cups_lede +libiconv-full +glib2  +libcupsimage_lede 
  URL:=http://gimp-print.sourceforge.net/
  CATEGORY:=Printing
endef


define Package/gutenprint_lede/description
	Gutenprint CUPS drivers for a wide range of Canon, Epson, HP and
	compatible printers.
endef

CONFIGURE_ARGS+= \
	--disable-nls \
	--disable-test \
	--without-doc \
	--without-readline \
	--without-foomatic \
	--without-foomatic3 \
	--enable-escputil=no \
	--enable-test=no \
	--disable-translated-cups-ppds \
	--disable-globalized-cups-ppds \
	--enable-simplified-cups-ppds \
	--bindir=/usr/bin \
	--sbindir=/usr/bin \
	--prefix=/usr \
	--exec-prefix=/usr

# Patch the Makefiles to run the binary from host compile
define Build/Compile
	$(SED) "s#\.\(/extract-strings\)#$(STAGING_DIR_HOSTPKG)/bin\1#" $(PKG_BUILD_DIR)/src/xml/Makefile
	$(SED) "s#\.\(/check_duplicate_printers.test\)#$(STAGING_DIR_HOSTPKG)/bin\1#" $(PKG_BUILD_DIR)/src/xml/printers/Makefile

	(cd $(PKG_BUILD_DIR) && \
	$(RM) src/xml/extract-strings && \
	echo '#!/bin/sh' > src/xml/extract-strings && \
	echo 'exec $(STAGING_DIR_HOSTPKG)/bin/extract-strings  "$$$$@" ' \
	>> src/xml/extract-strings && chmod +x src/xml/extract-strings && cp src/xml/extract-strings /tmp/)
	
	$(call Build/Compile/Default)
endef

define Package/gutenprint_lede/install
	$(INSTALL_DIR) $(1)/usr/sbin $(1)/usr/bin
	# XXX Do not install cups-genppdupdate, as it requires Perl + some perl libs
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/cups-genppd.[0-9]*.[0-9]* $(1)/usr/sbin/
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/cups-calibrate $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/usr/lib/cups/driver $(1)/usr/lib/cups/filter
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/cups/driver/gutenprint.* $(1)/usr/lib/cups/driver
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/cups/filter/* $(1)/usr/lib/cups/filter
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/libgutenprint.* $(1)/usr/lib
	$(INSTALL_DIR) $(1)/usr/share/cups/mime
	$(CP) $(PKG_INSTALL_DIR)/etc/cups/command.types $(1)/usr/share/cups/mime
	$(INSTALL_DIR) $(1)/usr/share/gutenprint
	$(CP) $(PKG_INSTALL_DIR)/usr/share/gutenprint/[0-9]*.[0-9]* $(1)/usr/share/gutenprint
endef

define Host/Compile
	$(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR)/src/xml $(HOST_MAKE_FLAGS) extract-strings
	$(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR)/src/xml/printers $(HOST_MAKE_FLAGS) check_duplicate_printers.test
endef

define Host/Install
        $(INSTALL_DIR) $(STAGING_DIR_HOSTPKG)/bin/
	# add host tools suffix, prevent conflicts with krb5
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/xml/.libs/extract-strings $(STAGING_DIR_HOSTPKG)/bin/extract-strings
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/xml/printers/.libs/check_duplicate_printers.test $(STAGING_DIR_HOSTPKG)/bin/check_duplicate_printers.test
endef

$(eval $(call HostBuild))
$(eval $(call BuildPackage,gutenprint_lede))

